package com.tavenli.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tavenli.entity.F18Device;
import com.tavenli.repository.F18DeviceDao;
import com.tavenli.repository.RedisDaoF18Device;
import com.tavenli.utils.ConstantsUtil;
import com.tavenli.utils.JsonDateValueProcessor;
import com.tavenli.vo.F18DeviceVo;

@Service(value = "f18DeviceService")
public class F18DeviceService {
	private static Logger logger = LoggerFactory
			.getLogger(UCenterService.class);

	@Autowired
	private F18DeviceDao f18DeviceDao;
	@Autowired
	private RedisDaoF18Device redisDaoF18Device;

	/*
	 * public List<F18DeviceVo> getDevStatus() {
	 * 
	 * }
	 */

	public F18Device getF18DeviceFromRedis(String deviceSn) {
		String str = (String) redisDaoF18Device.hashGet(
				ConstantsUtil.REDIS_DEVICE_HASH, deviceSn);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setRootClass(F18Device.class);
		Map<String, Class> classMap = new HashMap<String, Class>();
		classMap.put("f18Device", F18Device.class); // 指定JsonRpcRequest的request字段的内部类型
		jsonConfig.setClassMap(classMap);
		JSONObject f18DeviceObj = JSONObject.fromObject(str);
		F18Device f18Device = (F18Device) JSONObject.toBean(f18DeviceObj,
				jsonConfig);
		if (null == f18Device) {
			try {
				f18Device = f18DeviceDao.getSingleF18Device(deviceSn);
				if (null != f18Device)
					setF18DeviceToRedis(f18Device);
			} catch (Exception e) {
				return null;
			}
		}
		return f18Device;
	}

	public void setF18DeviceToRedis(F18Device f18Device) {
		JSONObject jsonObject = JSONObject.fromObject(f18Device);
		redisDaoF18Device.hashPut(ConstantsUtil.REDIS_DEVICE_HASH,
				f18Device.getDeviceSn(), jsonObject.toString());
	}

	/**
	 * 保存设备传输上来的考勤记录及其人员信息，由其它线程进行解析，这里只保存到redis中即可
	 * 
	 * @param logDataString
	 */
	public void saveLogDataToRedis(String logDataString, String sn) {
		String[] logDataArray = logDataString.split("\n");
		for (int i = 0; i < logDataArray.length; i++) {
			if (!"".equals(logDataArray[i])) {
				redisDaoF18Device.listLeftPush(ConstantsUtil.REDIS_DEVICE_LOG_LIST + sn,logDataArray[i]);
			}
		}
	}

	/**
	 * 更新设备为上线状态
	 * 
	 * @param sn
	 */
	public void UpdateDeviceOnLine(String sn) {
		/*
		 * JSONObject jsonObject=new JSONObject(); jsonObject.put("sn", sn);
		 * jsonObject.put("status", 1); jsonObject.put("lastUpdateDate", new
		 * Date());
		 */
		F18DeviceVo f18DeviceVo = new F18DeviceVo();
		f18DeviceVo.setDeviceSn(sn);
		f18DeviceVo.setIsOnline(1);
		f18DeviceVo.setLastUpdateDate(new Date());
		redisDaoF18Device.hashPut(ConstantsUtil.REDIS_DEVICE_STATUS_HASH, sn,
				JSONObject.fromObject(f18DeviceVo).toString());
	}

	/**
	 * 更新设备为掉线状态
	 * 
	 * @param sn
	 */
	public void UpdateDeviceOffLine(String sn) {
		/*
		 * JSONObject jsonObject=new JSONObject(); jsonObject.put("sn", sn);
		 * jsonObject.put("status", 0); jsonObject.put("lastUpdateDate", new
		 * Date());
		 */
		F18DeviceVo f18DeviceVo = new F18DeviceVo();
		f18DeviceVo.setDeviceSn(sn);
		f18DeviceVo.setIsOnline(0);
		f18DeviceVo.setLastUpdateDate(new Date());
		redisDaoF18Device.hashPut(ConstantsUtil.REDIS_DEVICE_STATUS_HASH, sn,
				JSONObject.fromObject(f18DeviceVo).toString());
	}

	public List<F18DeviceVo> getAllDevStatus() {
		List<F18DeviceVo> listF18DeviceVo = new ArrayList<F18DeviceVo>();
		Map<Object, Object> map = redisDaoF18Device
				.hashGetAll(ConstantsUtil.REDIS_DEVICE_STATUS_HASH);
		if (null != map) {
			for (Object key : map.keySet()) {
				Object value = (Object) map.get(key);
				JsonConfig jsonConfig = new JsonConfig();
				jsonConfig.setRootClass(F18DeviceVo.class);
				Map<String, Class> classMap = new HashMap<String, Class>();
				jsonConfig.registerJsonValueProcessor(Date.class,
						new JsonDateValueProcessor());
				classMap.put("jsonObject", F18DeviceVo.class); // 指定JsonRpcRequest的request字段的内部类型
				jsonConfig.setClassMap(classMap);
				JSONObject f18DeviceObj = JSONObject.fromObject(value,
						jsonConfig);
				F18DeviceVo f18DeviceVo = (F18DeviceVo) JSONObject.toBean(
						f18DeviceObj, jsonConfig);
				listF18DeviceVo.add(f18DeviceVo);
			}
		}
		return listF18DeviceVo;
	}

}
